1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution(object): def myPow(self, x, n): """ :type x: float :type n: int :rtype: float """ if n == 0: return 1.0 # 返回float elif n < 0: return 1 / self.myPow(x, -n) elif n % 2: # 结果为1,奇数 return self.myPow(x*x,n/2)*x else: # 偶数 return self.myPow(x*x,n/2)
|